const db = wx.cloud.database() //拿到数据库的引用
const card = db.collection("cards") //拿到集合的引用
const user = db.collection("users") //拿到集合的引用
var QRCode = require('../../utils/weapp-qrcode.js');
Page({
  data: {
    cards: [],
    isLoading: true,
    user_id: '',
    show: false,
  },

  onLoad() {

    this.setData({
      isLoading: true
    });
    this.getcards()

  },
  // async getUser() {
  //   const that = this
  //   const open_id = wx.getStorageSync('open_id')
  //   user.where({
  //     open_id: open_id
  //   }).get({
  //     success: res => {
  //       that.setData({
  //         user_id: res.data._id
  //       })
  //       this.getcards()
  //     }
  //   })
  // },

  async getcards() {
    const that = this
    card.where({
      shop_owner_open_id: wx.getStorageSync('open_id')
    }).get({
      success: (res) => {
        console.log(res)
        that.setData({
          cards: res.data,
          isLoading: false
        })
      }
    })
  },

  async fetchGoodsList() {

    const res = await wx.cloud.callFunction({
      name: 'quickstartFunctions',
      data: {
        type: 'fetchGoodsList'
      },
    });
    const goodsList = res?.result?.dataList || [];
    this.setData({
      isLoading: false,
      goodsList
    });
  },

  async generateMPCode() {
    wx.showLoading();
    const resp = await wx.cloud.callFunction({
      name: 'quickstartFunctions',
      data: {
        type: 'genMpQrcode',
        pagePath: 'pages/goods-list/index',
      }
    });
    this.setData({
      codeModalVisible: true,
      codeImageSrc: resp?.result
    });
    wx.hideLoading();
  },

  show(e) {
    console.log(e.currentTarget.dataset.card._id)
    this.setData({
      show: true
    })

    this.createCode(e.currentTarget.dataset.card._id);
  },

  createCode(id) {
    var that = this;
    new QRCode('myCanvas', {
      text: id,
      width: 300, //canvas 画布的宽
      height: 300, //canvas 画布的高
      padding: 0, // 生成二维码四周自动留边宽度，不传入默认为0
      correctLevel: QRCode.CorrectLevel.H, // 二维码可辨识度
      // colorDark: "#6bad6f", //分别为两种交替颜色
      // colorLight: "white",
      // callback: (res) => {
        //工具回调数据
        // wx.hideLoading()
        // console.log("生成二维码", res)
        //将图片路劲放入data中，显示在wxml的image标签上
        // that.setData({
        //   imagePath: res.path
        // })
      // }
    })
  },

  onClose() {
    this.setData({
      show: false
    });
  },
});